<template>
    <el-dialog
        v-model="showflag"
        :title="title"
        width="30vw"
        align-center
        :close-on-click-modal="false"
        @close="resetdata">
        <div style="padding: 0 40px">
            <el-form
                ref="ruleFormRef"
                style="width: 100%"
                :model="ruleForm"
                :rules="rules"
                label-width="auto"
                class="demo-ruleForm"
                :size="formSize"
                status-icon>
                <el-form-item label="姓名" prop="name">
                    <el-input
                        v-model="ruleForm.name"
                        :disabled="!isedit"
                        placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                    <el-input
                        v-model="ruleForm.phone"
                        :disabled="!isedit"
                        placeholder="请输入手机号" />
                </el-form-item>
            </el-form>
        </div>
        <template #footer>
            <div class="dialog-footer">
                <el-button @click="showflag = false">取消</el-button>
                <el-button v-if="isedit" type="primary" @click="submitForm(ruleFormRef)">
                    确定
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { onMounted, ref, provide, reactive, toRaw, inject } from "vue";
import { ElMessage } from "element-plus";
import { post, baseURL } from "../../../../api/request";
import type { FormInstance, FormRules } from "element-plus";
let showflag = ref(false);
let title = ref("新增人员");
let isedit = ref(true);
let device_id = ref("");
let token = ref("");
const getdata = inject<() => void>("getdata")!; //父组件的方法

onMounted(() => {
    token.value = localStorage.getItem("token") || "";
});

//表单相关---------------------------------------

interface RuleForm {
    name: string;
    phone: string;
    id: string;
}

const formSize = ref("default");
const ruleFormRef = ref<FormInstance>();
let ruleForm = reactive<RuleForm>({
    id: "",
    name: "",
    phone: "",
});

const rules = reactive<FormRules<RuleForm>>({
    name: [{ required: true, message: "请填写姓名", trigger: "blur" }],
    phone: [{ required: true, message: "请填写手机号", trigger: "blur" }],
});

const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return;
    await formEl.validate((valid, fields) => {
        if (valid) {
            post({
                url: ruleForm.id
                    ? "/api/rygl/edit"
                    : "/api/rygl/add",
                data: toRaw(ruleForm),
            }).then((res: any) => {
                ElMessage.success("操作成功!");
                getdata();
                showflag.value = false;
                resetdata();
            });
        } else {
            console.log("error submit!");
        }
    });
};
//表单END---------------------------------------

const resetdata = () => {
    Object.assign(ruleForm, {
        id: "",
        name: "",
        phone: "",
    });
    isedit.value = true;
    title.value = "新增人员";
};

// 通过 defineExpose 暴露给父组件
defineExpose({
    showflag,
    title,
    isedit,
    device_id,
    ruleForm,
});
</script>

<style lang="scss" scoped>
:deep(.el-form-item) {
    margin-bottom: 30px;
    width: 100%;
}
.linebox {
    display: flex;
    align-items: flex-start;
    width: 100%;
}
:deep(.avatar-uploader .avatar) {
    width: 178px;
    height: 178px;
    display: block;
}
:deep(.avatar-uploader .el-upload) {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

:deep(.avatar-uploader .el-upload:hover) {
    border-color: var(--el-color-primary);
}

:deep(.el-icon.avatar-uploader-icon) {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>
